// 菜单组件
// 底部版权组件
<template>
  <div id="mymenu" style="background-color:#1296db;">
    <div id="meanitems">
      <el-menu
        style="z-index:1000"
        :default-active="activeIndex"
        background-color="#1296db"
        mode="horizontal"
        text-color="#FFF"
        active-text-color="#ffd04b"
      >
        <el-menu-item
          class="active"
          :key="menu.id"
          v-for="(menu,index) in menuDatas"
          :index="index+1+''"
          @click="pushto(menu.url,index+1)"
        >{{menu.menuName}}</el-menu-item>
      </el-menu>
    </div>
    <div id="searchbox" style="z-index:1001">
      <el-autocomplete
        size="small"
        @select="handleSelect"
        v-model="searchText"
        placeholder="您可搜素菜单、功能..."
        prefix-icon="el-icon-search"
        :fetch-suggestions="querySearch"
      ></el-autocomplete>
    </div>
  </div>
</template>

<script>
export default {
  name: "menuX",
  data() {
    return {
      searchText: ""
    };
  },

  created() {
    console.log(this.$store.state.m_menu.menuState.firstLevel);

    this.$api.menu.getMenus(this.$store.state.m_user.userInfo.id).then(res => {
      if (res.code == 200) {
        this.$store.commit("initMenuInfo", res.data);
        // this.$store.commit("initMentState",{firstLevel:"1"})
      }
    });
	},
	
	watch:{
		$route(to,from){
			let topath = to.path;
			this.menuDatas.forEach((item,index)=>{
				if(topath.indexOf(item.url)>=0){
					this.$store.commit("setMentState", { firstLevel: String(index+1) });
				}
			});
		}
	},

  computed: {
    activeIndex: function() {
      return this.$store.state.m_menu.menuState.firstLevel;
    },
    menuDatas() {
      var data = this.$store.state.m_menu.menuInfo;
      var result = [];
      data.forEach(element => {
        result.push({
          menuId: element.menuId,
          menuName: element.menuName,
          parentName: element.parentName,
          parentId: element.parentId,
          orderNum: element.orderNum,
          url: element.url,
          menuType: element.menuType,
          visible: element.visible,
          perms: element.perms,
          icon: element.icon
        });
      });
      return result;
    }
  },

  methods: {
    querySearch(queryString, cb) {
      let data = this.$store.state.m_menu.menuInfo;
      let result = [];
      data.forEach(item => {
        item.children.forEach(ii => {
          result.push({ value: ii.menuName, url: ii.url });
        });
      });
      var results = queryString
        ? result.filter(item => {
            return item.value.indexOf(queryString) >= 0;
          })
        : result;
      cb(results);
    },

    handleSelect(key) {
      console.log(key);
      this.$router.push(key.url);
    },

    pushto(path, index) {
      this.$store.commit("setMentState", { firstLevel: String(index) });
      this.$router.push(path);
    }
  }
};
</script>

<style>
#mymenu {
  display: flex;
  justify-content: space-between;
}
#menu {
  height: 40px;
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  line-height: 250%;
  position: absolute;
  /* top: 0; */
  left: 0;
  right: 0;
  align-items: center;
  display: -webkit-flex;
  /* border-bottom: medium solid #006eab; */
}
#menuitems {
  height: 100%;
  width: 80%;
  float: left;
}
#searchbox {
  width: 20%;
  float: right;
  margin-right: 10px;
  vertical-align: middle;
  display: flex;
  flex-direction: column;
  justify-content: center;
}
.el-menu {
  height: 100%;
}
.el-menu-item {
  height: 100% !important;
  line-height: 40px !important;
  /* margin-top: 5px !important; */
}
</style>
